package projectsupporter.common.operation;

import java.lang.reflect.InvocationTargetException;

import jxl.Hyperlink;
import jxl.Sheet;
import jxl.Workbook;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Shell;

import projectsupporter.form.MainForm;

import dbTools.DBToolEngine;

public class LoadTableInfoOperation implements IRunnableWithProgress{
	
	private DBToolEngine engine; 
	
	private MainForm form;

	public LoadTableInfoOperation(MainForm form,String fileName) {
		engine=new DBToolEngine(fileName);
		this.form=form;
	}



	@Override
	public void run(IProgressMonitor monitor) throws InvocationTargetException,
			InterruptedException {
		
		Workbook workbook = engine.getWorkBook();
		
		System.out.println("共有" +workbook.getNumberOfSheets() + "sheets");
		
		Sheet mainSheet = workbook.getSheet("テーブル一覧");
		
		if(mainSheet==null){
			return ;
		}
	   Hyperlink[] tableLinks =mainSheet.getHyperlinks();
		
		monitor.beginTask("Loding table info", tableLinks.length);
		
		for(Hyperlink link:tableLinks)
		{
			monitor.subTask(engine.fillTableInfo(workbook, mainSheet, link));
			monitor.worked(1);
		}
	    monitor.done();
		
	    if(DBToolEngine.TABLE_INFO_MAP!=null)
	    {
	    	form.getsShell().getDisplay().asyncExec(new Runnable() {
				public void run() {
					
					form.fillTableTree(DBToolEngine.TABLE_INFO_MAP);
					
				}
			});
	    	
	    	
	    }
	    
	}

}
